package com.hb.core.base;

import com.hb.core.constant.Constants;
import com.hb.util.Results;

import java.io.Serializable;

/**
 * @description:
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/9/2 15:55
 * @version:1.0
 */
public class BaseResponse<T> implements Serializable {

    private String code;

    private String msg;

    private T data;

    public BaseResponse() {
    }

    public BaseResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.code = "0";
        response.msg = "成功";
        response.data = data;
        return response;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> response = new BaseResponse<>();
        response.code = "0";
        response.msg = "成功";
        response.data = null;
        return response;
    }

    public static BaseResponse fail(String msg) {
        BaseResponse response = new BaseResponse();
        response.code = "-1";
        response.msg = msg;
        return response;
    }

    public static BaseResponse fail(String code, String msg) {
        BaseResponse response = new BaseResponse();
        response.code = code;
        response.msg = msg;
        return response;
    }

    public static <T> BaseResponse<T> fail(String code, String msg, T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.code = code;
        response.msg = msg;
        response.data = data;
        return response;
    }

    public static BaseResponse fail(BaseResponse baseResponse){
        BaseResponse response = new BaseResponse();
        response.code = baseResponse.getCode();
        response.msg = baseResponse.getMsg();
        return response;
    }

    public boolean isSuccess(){
        return "0".equals(this.code);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
